from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView

from apps.goods.models import SKU,GoodsCategory
from apps.meiduo_admin.serializers.sku import SKUKSerializer,ThreeCategorySerializer, SimpleSPUSerializer,SPUSpecsSerializer
from apps.meiduo_admin.utils import PageNum
from apps.goods.models import SPU,SPUSpecification


class SKUModelViewSet(ModelViewSet):

    # 指定查询集
    # queryset = SKU.objects.all()
    # 同样根据keyword参数来判断
    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        else:
            return SKU.objects.all()


    # 指定序列化器
    serializer_class = SKUKSerializer

    # 指定分液器
    pagination_class = PageNum


############获取三级分类##################
class ThreeCategoryListAPIView(ListAPIView):

    # 指定查询集
    queryset = GoodsCategory.objects.filter(subs=None)

    # 指定序列化器
    serializer_class = ThreeCategorySerializer


############获取SPU####################
class SimpleSPUListAPIView(ListAPIView):

    # 指定序列化器
    serializer_class = SimpleSPUSerializer
    # 指定查询集
    queryset = SPU.objects.all()


class SPUSpecsAPIView(APIView):
    """
    商品规格信息
    """
    def get(self,request,pk):

        # pk 就是 spu_id
        specs = SPUSpecification.objects.filter(spu_id=pk)
        # 指定序列化器
        serializer = SPUSpecsSerializer(specs,many=True)
        return Response(serializer.data)